﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Einstein.Data;
using Einstein.Group.DAL;
using System.Data;

namespace Einstein.Group.BLL
{
    public class DictionaryBLL
    {
        public static List<Dictionary> SelectAll()
        {
            return new DictionaryDAL().SelectAll();
        }

        public static List<Dictionary> GetByCategory(string category)
        {
            return new DictionaryDAL().GetByCategory(category);
        }

        public static Dictionary GetByCategoryAndKeyName(string category, string keyName)
        {
            return new DictionaryDAL().GetByCategoryAndKeyName(category, keyName);
        }

        public static Dictionary GetByCategoryAndKeyValue(string category, int keyValue)
        {
            return new DictionaryDAL().GetByCategoryAndKeyValue(category, keyValue);
        }


        public static bool ExistCategory(Dictionary dic)
        {
            return new DictionaryDAL().ExistCategory(dic);
        }

        public static bool ExistKeyName(Dictionary dic, bool isEdit)
        {
            return new DictionaryDAL().ExistKeyName(dic, isEdit);
        }

        public static DataSet GetDataSet()
        {

            var all = new DictionaryDAL().SelectAll();
            var _DataSet = new DataSet();

            DataTable dtMain = GenerateDtForItems();
            dtMain.TableName = "Main";
            DataTable dtSubMain = GenerateDt();
            dtSubMain.TableName = "Sub";

            var groupCategory = from item in all
                                group item by item.Category into view
                                select new
                                {
                                    Category = view.Key,
                                    Count = view.Count()
                                };



            foreach (var d in groupCategory)
            {
                var dr = dtMain.NewRow();
                dr["Count"] = d.Count;
                dr["Category"] = d.Category;
                dtMain.Rows.Add(dr);


                var subAll = all.Where(c => c.Category == d.Category);

                foreach (var s in subAll)
                {
                    var drsubMain = dtSubMain.NewRow();
                    drsubMain["KeyName"] = s.KeyName;
                    drsubMain["Category"] = s.Category;
                    drsubMain["KeyValue"] = s.KeyValue;
                    drsubMain["ID"] = s.ID;
                    dtSubMain.Rows.Add(drsubMain);
                }
            }

            _DataSet.Tables.Add(dtMain);
            _DataSet.Tables.Add(dtSubMain);


            _DataSet.Relations.Add("1", _DataSet.Tables["Main"].Columns["Category"],
                                           _DataSet.Tables["Sub"].Columns["Category"], false);

            return _DataSet;
        }


        public static bool Update(Dictionary info)
        {
            return new DictionaryDAL().Update(info);
        }
        public static bool Delete(Dictionary info)
        {
            return new DictionaryDAL().Delete(info);
        }
        public static bool Add(Dictionary info)
        {
            return new DictionaryDAL().Add(info);
        }
        public static bool UpdateCategory(string newcategory, string oldcategory)
        {
            return new DictionaryDAL().UpdateCategory(newcategory, oldcategory);
        }
        public static bool DeteteByCategory(Dictionary info)
        {
            return new DictionaryDAL().DeteteByCategory(info);
        }


        private static DataTable GenerateDtForItems()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Category", typeof(System.String)));
            dt.Columns.Add(new DataColumn("Count", typeof(System.Int32)) { Caption = "个数" });
            return dt;
        }
        private static DataTable GenerateDt()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("ID", typeof(System.Int32)));
            dt.Columns.Add(new DataColumn("Category", typeof(System.String)));
            dt.Columns.Add(new DataColumn("KeyName", typeof(System.String)));
            dt.Columns.Add(new DataColumn("KeyValue", typeof(System.Int32)));
            return dt;
        }





    }
}
